#
# Great Doctor Mod
# CvGDEventManager
# 

from CvPythonExtensions import *
import CvUtil

import CvEventManager
import sys
import PyHelpers
import FieldHospitalUtils
import CvConfigParser

import pickle

################# SD-UTILITY-PACK ###################
import SdToolKit
sdEcho         = SdToolKit.sdEcho
sdModInit      = SdToolKit.sdModInit
sdModLoad      = SdToolKit.sdModLoad
sdModSave      = SdToolKit.sdModSave
sdEntityInit   = SdToolKit.sdEntityInit
sdEntityExists = SdToolKit.sdEntityExists
sdGetVal       = SdToolKit.sdGetVal
sdSetVal       = SdToolKit.sdSetVal

gc = CyGlobalContext()	
objFieldHospitalUtils = FieldHospitalUtils.FieldHospitalUtils()

PyPlayer = PyHelpers.PyPlayer
PyGame = PyHelpers.PyGame()


# Change this to true if great doctors should be consumed after building a
# field hospital.
# Default value is false
g_bBuildingFieldHospitalConsumesGreatDoctor = False

# globals
###################################################
class CvFieldHospitalEventManager:
	def __init__(self, eventManager):
		# initialize base class

		eventManager.addEventHandler("improvementBuilt", self.onImprovementBuilt)
		eventManager.addEventHandler("BeginGameTurn",self.onBeginGameTurn)

		global g_bBuildingFieldHospitalConsumesGreatDoctor
		
		config = CvConfigParser.CvConfigParser("ViSa_v3.ini")

		if(config != None):
			g_bBuildingFieldHospitalConsumesGreatDoctor = config.getboolean("Great Doctor", "Building Field Hospital Consumes Great Doctor", False)

			
	# Called when an improvement is built in the game. If the improvement built
	# was a field hospital then the field hospital location information is saved.
	def onImprovementBuilt(self, argsList):
		'Improvement Built'
		iImprovement, iX, iY = argsList
		
		iGreatDoctor = gc.getInfoTypeForString("UNIT_DOCTOR")
		
		iFieldHospital = gc.getInfoTypeForString("IMPROVEMENT_FIELD_HOSPITAL")

		# If the improvement is a field hospital then save it's location.
		if(iImprovement == iFieldHospital):
			objFieldHospitalUtils.saveFieldHospitalData(iX, iY)

			# If the mod is configured to consume great doctors when they finish 
			# building the field hospital then get the plot where the field hospital
			# was built, find the doctor in the plot and kill them off.
			if(g_bBuildingFieldHospitalConsumesGreatDoctor):
				plot = gc.getMap().plot(iX, iY)

				# Get the units on the plot
				unitList = objFieldHospitalUtils.getPlotUnitList(plot)
				
				# Go throught the units in the plot
				for unit in unitList:
				
					# HACK: Fix this since we are just killing off the first 
					# doctor we find.
					if(unit.getUnitType() == iGreatDoctor):
						unit.kill(false)
						break


	# Called at the beginning of each game turn. The method will get the field
	# hospital location information, check to make sure if the field hospital
	# still exists and heals units where appropriate.
	def onBeginGameTurn(self, argsList):
		'Called at the beginning of the end of each turn'

		# Setup the mercenary data structure if it hasn't been setup yet.
		if(sdEntityExists("Great Doctor", "FieldHospitalData") == False):
			objFieldHospitalUtils.setupFieldHospitalData()

		# Get the field hospital location information	
		fieldHospitalData = sdGetVal("Great Doctor","FieldHospitalData","FieldHospitalData")
		
		removalList = []
		
		# Go through all of the possible field hospital location information
		for key in fieldHospitalData.iterkeys():
			iX, iY = fieldHospitalData[key]
			plot = gc.getMap().plot(iX, iY)
			
			# If the plot doesn't contain a field hospital add the location to 
			# the removalList and continue
			if(not objFieldHospitalUtils.plotContainsFieldHospital(plot)):
				removalList.append(key)
				continue

			# Heal the units found in the plot
			objFieldHospitalUtils.healUnits(plot)
			
			
		# Go through the items in the removalList and delete them from the 
		# fieldHospitalData dictionary
		for i in range(len(removalList)):
			del fieldHospitalData[removalList[i]]

		# Update the field hospital data only if one or more items were removed
		if(len(removalList) > 0):
			sdSetVal("Great Doctor","FieldHospitalData","FieldHospitalData",fieldHospitalData)
			
			
